package com.project2.controller;


import com.project2.bean.CommonData;
import com.project2.bean.PageBo;
import com.project2.bean.ResultVo;
import com.project2.bean.bo.PermissionBO;
import com.project2.bean.po.MarketRole;
import com.project2.bean.po.MarketRoleExample;
import com.project2.bean.vo.PermissionListVO;
import com.project2.bean.vo.RoleOptionsVo;
import com.project2.mapper.MarketRoleMapper;
import com.project2.service.AdminRoleService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author : bunnyupup
 * @description :
 * @date : Created in 2022/8/12 19:26
 */
@RestController
@RequestMapping("admin/role")
public class AdminRoleController {
    @Autowired
    AdminRoleService adminRoleService;

    @Autowired
    MarketRoleMapper marketRoleMapper;

    /**
     * 显示所有角色或根据姓名查询角色
     * @param name
     * @param bo
     * @return
     */
    @RequiresPermissions("admin:role:list")
    @RequestMapping("list")
    public ResultVo list(String name, PageBo bo){
        CommonData<MarketRole> list = adminRoleService.list(name, bo);
        return ResultVo.ok(list);

    }

    /**
     * 返回角色信息
     * @return
     */
    @RequestMapping("options")
    public ResultVo options(){
        CommonData<RoleOptionsVo> options = adminRoleService.options();
        ResultVo resultVo = ResultVo.ok(options);
        return resultVo;
    }

    /**
     * 创建角色
     * 还未开发：重名判断
     * @param marketRole
     * @return
     */
    @RequiresPermissions("admin:role:create")
    @RequestMapping("create")
    public ResultVo create(@RequestBody MarketRole marketRole){
        // 首先进行重名判断
        MarketRoleExample example = new MarketRoleExample();
        MarketRoleExample.Criteria criteria = example.createCriteria();
        criteria.andNameEqualTo(marketRole.getName());
        List<MarketRole> marketRoles = marketRoleMapper.selectByExample(example);
        if(marketRoles.size()!=0){
            // 说明有重名
            return ResultVo.wrong("角色重名，请修改名称！");
        }

        MarketRole marketRole1 = adminRoleService.create(marketRole);
        return ResultVo.ok(marketRole1);
    }

    /**
     * 修改角色信息
     * 还未开发：重名判断
     * @param marketRole
     * @return
     */
    @RequiresPermissions("admin:role:update")
    @RequestMapping("update")
    public ResultVo update(@RequestBody MarketRole marketRole){
        // 首先进行重名判断
        MarketRoleExample example = new MarketRoleExample();
        MarketRoleExample.Criteria criteria = example.createCriteria();
        criteria.andNameEqualTo(marketRole.getName());
        List<MarketRole> marketRoles = marketRoleMapper.selectByExample(example);
        if(marketRoles.size()!=0){
            // 说明有重名
            return ResultVo.wrong("角色重名，请修改名称！");
        }
        adminRoleService.update(marketRole);
        return ResultVo.ok();
    }

    /**
     * 删除角色
     * 还未开发：是否有删除权限 只有超管能进入，不用判断了
     * @param marketRole
     * @return
     */
    @RequiresPermissions("admin:role:delete")
    @RequestMapping("delete")
    public ResultVo delete(@RequestBody MarketRole marketRole){
        adminRoleService.delete(marketRole);
        return ResultVo.ok();
    }

    /**
     * 根据roleId显示这个id对应所有权限api以及显示所有的权限
     * @param roleId
     * @return
     */
    @RequiresPermissions("admin:role:permission:get")
    @GetMapping("permissions")
    public ResultVo permissions(String roleId){
        PermissionListVO permissionListVO = adminRoleService.getPermissions(roleId);
        return ResultVo.ok(permissionListVO);
    }

    /**
     * 更新权限
     * @return
     */
    @RequiresPermissions("admin:role:permission:update")
    @PostMapping("permissions")
    public ResultVo permissions(@RequestBody PermissionBO bo){
        adminRoleService.updatePermissions(bo);
        return ResultVo.ok();
    }



}
